package right_content

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"gitee.com/huanminabc/go-utils/common"
	"gitee.com/huanminabc/go-utils/db"
	log "github.com/sirupsen/logrus"
	"go-CodeToolCmd/java/code_generator"
	"go-CodeToolUi/core"
	"go-CodeToolUi/resources"
	"go-CodeToolUi/utils"
	"os"
)

type MybaitisPlusPrcCode struct {
	root           fyne.CanvasObject
	poToDto        bool
	po             bool
	dto            bool
	manager        bool
	mapper         bool
	provider       bool
	api            bool
	packageName    *widget.Entry  //包路径
	rpcPackageName *widget.Entry  //rpc包路径
	dbType         *widget.Select //数据库类型
	dbName         *widget.Entry  //数据库名称
	tableName      *widget.Entry  //表名称
	outFilePath    string         //输出文件路径
}

func (o *MybaitisPlusPrcCode) LadeContent() {
	userHome, _ := os.UserHomeDir()
	o.outFilePath = userHome + string(os.PathSeparator) + "codeTemplate"

	o.root = container.New(layout.NewVBoxLayout(),
		o.row0_1(),
		utils.Line(),
		utils.RowEmpty(20),
		o.row0_2(),
		utils.RowEmpty(20),
		o.row0_3(),
		utils.RowEmpty(20),
		o.row0_4(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		o.row1_1(),
		utils.Line(),
		utils.RowEmpty(20),
		o.row1_2(),
		utils.RowEmpty(20),
		o.row1_3(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		o.row2_1(),
		utils.Line(),
		utils.RowEmpty(20),
		o.row2_2(),
		utils.RowEmpty(20),
		o.row2_3(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		o.row3_1(),
		utils.RowEmpty(20),
		o.row4_1(),
		utils.RowEmpty(30),
	)
}

func (o *MybaitisPlusPrcCode) GetContent() fyne.CanvasObject {
	return o.root
}

func (a *MybaitisPlusPrcCode) row0_1() fyne.CanvasObject {
	label1 := &canvas.Text{
		Text:      "配置:",
		TextStyle: fyne.TextStyle{Bold: true},
		TextSize:  16,
		Color:     common.HexToRGB("#000000"), //纯黑色
	}
	return container.NewBorder(nil, nil, utils.ColumnEmpty(80), utils.ColumnEmpty(100), label1)
}
func (a *MybaitisPlusPrcCode) row0_2() fyne.CanvasObject {
	label1 := widget.NewLabel("数据库类型:")
	//获取map的Value
	selectOption1 := widget.NewSelect([]string{
		"mysql",
		"oracle",
	}, nil)
	selectOption1.PlaceHolder = "请选择需要的类型"
	a.dbType = selectOption1
	f := container.New(layout.NewFormLayout(), label1, selectOption1)
	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100), f)
}

func (a *MybaitisPlusPrcCode) row0_3() fyne.CanvasObject {
	label1 := widget.NewLabel("数据库:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入数据库名称"
	a.dbName = input
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MybaitisPlusPrcCode) row0_4() fyne.CanvasObject {
	label1 := widget.NewLabel("表名称:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入表名称"
	a.tableName = input
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MybaitisPlusPrcCode) row1_1() fyne.CanvasObject {
	label1 := &canvas.Text{
		Text:      "libs:",
		TextStyle: fyne.TextStyle{Bold: true},
		TextSize:  16,
		Color:     common.HexToRGB("#000000"), //纯黑色
	}
	return container.NewBorder(nil, nil, utils.ColumnEmpty(80), utils.ColumnEmpty(100), label1)
}

func (a *MybaitisPlusPrcCode) row1_2() fyne.CanvasObject {
	check1 := widget.NewCheck("DTO生成", func(value bool) {
		a.dto = value
		log.Println("Check set to", value)
	})
	check2 := widget.NewCheck("API生成", func(value bool) {
		a.api = value
		log.Println("Check set to", value)
	})
	grid := container.New(layout.NewGridLayout(4), check1, check2)

	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100), grid)

}
func (a *MybaitisPlusPrcCode) row1_3() fyne.CanvasObject {
	label1 := widget.NewLabel("包路径:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入包路径"
	a.rpcPackageName = input
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MybaitisPlusPrcCode) row2_1() fyne.CanvasObject {
	label1 := &canvas.Text{
		Text:      "provider:",
		TextStyle: fyne.TextStyle{Bold: true},
		TextSize:  16,
		Color:     common.HexToRGB("#000000"), //纯黑色
	}
	return container.NewBorder(nil, nil, utils.ColumnEmpty(80), utils.ColumnEmpty(100), label1)
}

func (a *MybaitisPlusPrcCode) row2_2() fyne.CanvasObject {
	check1 := widget.NewCheck("PO转DTO", func(value bool) {
		a.poToDto = value
	})

	check2 := widget.NewCheck("PO生成", func(value bool) {
		a.po = value
	})

	check3 := widget.NewCheck("manager生成", func(value bool) {
		a.manager = value
	})
	check4 := widget.NewCheck("mapper生成", func(value bool) {
		a.mapper = value
	})
	check5 := widget.NewCheck("provider生成", func(value bool) {
		a.provider = value
	})
	grid := container.New(layout.NewGridLayout(4), check1, check2, check3, check4, check5)
	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100), grid)

}

func (a *MybaitisPlusPrcCode) row2_3() fyne.CanvasObject {
	label1 := widget.NewLabel("包路径:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入包路径(provider依赖libs包路径)"
	a.packageName = input
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MybaitisPlusPrcCode) row3_1() fyne.CanvasObject {
	button := widget.NewButton("生成结果", func() {
		connectLogo := db.ConnectLogo{
			Type:   db.ToDbType(a.dbType.Selected),
			User:   core.ConfigApp.DbConfig.MysqlConfig.User,
			Passwd: core.ConfigApp.DbConfig.MysqlConfig.Password,
			Host:   core.ConfigApp.DbConfig.MysqlConfig.Host,
			Port:   core.ConfigApp.DbConfig.MysqlConfig.Port,
			DbName: a.dbName.Text,
			Params: "charset=utf8mb4&parseTime=True&loc=Local",
		}
		dbCon := db.GetDb(connectLogo)
		if dbCon == nil {
			log.Error("连接数据库失败")
			utils.ErrorDialog("连接数据库失败", "请检查数据库配置是否正确", core.TopWin)
			return
		}
		defer dbCon.Close()
		table := db.GetTableInfo(connectLogo, a.tableName.Text)

		var isCheck = false //是否选中

		if a.dto {
			log.Println("DTO生成")
			java := resources.GetTemplateMybatisPlusDtoJava()
			code_generator.NewCreateDtoTemplate(a.rpcPackageName.Text, table, java, a.outFilePath+string(os.PathSeparator)+"libs")
			isCheck = true
		}
		if a.api {
			log.Println("API生成")
			java := resources.GetTemplateMybatisPlusRpcApiJava()
			code_generator.NewCreateRpcApiTemplate(a.rpcPackageName.Text, table, java, a.outFilePath+string(os.PathSeparator)+"libs")
			isCheck = true
		}
		if a.poToDto {
			log.Println("PO转DTO生成")
			//检查rpcPackageName是否为空
			if a.rpcPackageName.Text == "" {
				utils.ErrorDialog("生成失败", "【PO转DTO生成】的时候【libs】包路径不能为空", core.TopWin)
				return
			}
			java := resources.GetTemplateMybatisPlusConverterEntityToDtoJava()
			code_generator.NewCreatePoToDtoConverterApiTemplate(
				a.packageName.Text,
				a.rpcPackageName.Text,
				table, java, a.outFilePath+string(os.PathSeparator)+"provider")
			isCheck = true
		}
		if a.po {
			log.Println("PO生成")
			java := resources.GetTemplateMybatisPlusEntityJava()
			code_generator.NewCreateEntityTemplate(a.packageName.Text, table, java, a.outFilePath+string(os.PathSeparator)+"provider")
			isCheck = true
		}
		if a.manager {
			log.Println("manager生成")
			//检查rpcPackageName是否为空
			if a.rpcPackageName.Text == "" {
				utils.ErrorDialog("生成失败", "【manager生成】的时候【libs】包路径不能为空", core.TopWin)
				return
			}
			java := resources.GetTemplateMybatisPlusManagerJava()
			code_generator.NewCreateManagerTemplate(
				a.packageName.Text,
				a.rpcPackageName.Text,
				table, java, a.outFilePath+string(os.PathSeparator)+"provider")
			isCheck = true
		}
		if a.mapper {
			log.Println("mapper生成")
			java := resources.GetTemplateMybatisPlusMapperJava()
			code_generator.NewCreateMapperTemplate(a.packageName.Text, table, java, a.outFilePath+string(os.PathSeparator)+"provider")
			isCheck = true
		}
		if a.provider {
			log.Println("provider生成")

			//检查rpcPackageName是否为空
			if a.rpcPackageName.Text == "" {
				utils.ErrorDialog("生成失败", "【provider生成】的时候【libs】包路径不能为空", core.TopWin)
				return
			}

			java := resources.GetTemplateMybatisPlusRpcImplApiJava()
			code_generator.NewCreateRpcImplApiTemplate(
				a.packageName.Text,
				a.rpcPackageName.Text,
				table, java, a.outFilePath+string(os.PathSeparator)+"provider")
			isCheck = true
		}

		if !isCheck {
			utils.ErrorDialog("生成失败", "请选择生成类型", core.TopWin)
			return
		}
		utils.InfoDialog("代码生成成功", "请到对应路径下拿文件", core.TopWin)

	})
	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100),
		utils.Height(button, 50))
}

func (a *MybaitisPlusPrcCode) row4_1() fyne.CanvasObject {
	label1 := &canvas.Text{
		Text:      "文件输出路径:",
		TextStyle: fyne.TextStyle{Bold: false},
		TextSize:  16,
		Color:     common.HexToRGB("#E6421A"), //纯黑色
	}
	//获取用户目录
	label1.Text = "文件输出路径:" + a.outFilePath
	return container.NewCenter(label1)
}
